// Do file that extracts LISA data for the years 1991, 1994, 1998, 2002, 2006, 2010 and 2014
//
// Last changed: 2020-03-30 (KOL)
  
  
foreach y in 1990 1991 1994 1998 2002 2006 2010 2014  { 
  
  di `y'
  use "D:\SCB_ConPol\Stata\RTB\RTB_`y'.dta", clear
  bysort LopNr: egen cID = count(LopNr)
  drop if cID > 1
  drop cID
  
  merge 1:m LopNr using "D:\SCB_ConPol\Stata\RTB\Fodelseuppg", keepusing(FodArMan)
  keep if _merge == 3
  drop _merge 
  
  bysort LopNr: egen cID = count(LopNr)
  drop if cID > 1
  drop cID
  
  gen FodelseAr = floor(FodArMan/100)
  
// Civilstånd has different categories before and after 1998, harmonize the codes  
  
  
  
  if `y' < 1998 {
    
	tostring Civil, gen(sCivil)
	replace sCivil = "OG" if inlist(Civil, 1, 8, 9)
	replace sCivil = "G" if inlist(Civil, 2, 3, 7)
	replace sCivil = "S" if Civil == 4
	replace sCivil = "Ä" if Civil == 5
  }
  else {
    gen sCivil = Civil
  }
  
  merge 1:1 LopNr using "D:\SCB_ConPol\Stata\RTB\FodelselandIFAUGrupp.dta"
  keep if _merge == 3
  drop _merge
  
// Merge Bosnia and Yugoslavia, since it does not exists all years 
  replace IFAUkod = 30 if IFAUkod == 29
  
  gen Ar = `y'
  gen Vuxen = (Ar-FodelseAr) > 17 if FodelseAr <.
  tab Vuxen 
  
  bysort Kommun IFAUkod: egen Antal_FLxKom = count(LopNr)
  bysort Kommun: egen Storlek_Kom = count(LopNr)
  
  bysort Kommun IFAUkod: egen Antal_FLxKomVuxen = total(Vuxen)
  bysort Kommun: egen Storlek_KomVuxen = total(Vuxen)
  
  keep LopNr Forsamling sCivil Medblandnamn IFAUkod Antal_FLxKom* Storlek_Kom*
  compress
  tempfile RTB`y'
  save `RTB`y''
  
  use "D:\SCB_ConPol\Stata\LISA\LISA_`y'.dta", clear
  bysort LopNr: egen cID = count(LopNr)
  drop if cID > 1
  drop cID
  
  keep LopNr Sun2000niva Barn* SyssStat LoneInk ArbLos SocBidrFam BostBidrFam DispInkPersF 
  
  gen Ar = `y' 
  gen Sysselsatt = (SyssStat == 1) if SyssStat <.
  gen ArbetsLos = (ArbLos > 0) if ArbLos <.
  gen Socbidrag = (SocBidrFam > 0) if SocBidrFam <.
  
  gen UtbAr=.
  replace UtbAr=7 if Sun2000niva<200
  replace UtbAr=9 if inrange(Sun2000niva,200,299)
  replace UtbAr=9.5 if Sun2000niva==204
  replace UtbAr=10 if inrange(Sun2000niva,310,319)
  replace UtbAr=11 if inrange(Sun2000niva,320,329)
  replace UtbAr=12 if inrange(Sun2000niva,330,339)
  replace UtbAr=13 if inrange(Sun2000niva,410,419)
  replace UtbAr=14 if inrange(Sun2000niva,520,529)
  replace UtbAr=15 if inrange(Sun2000niva,530,539)
  replace UtbAr=16 if inrange(Sun2000niva,540,549)
  replace UtbAr=17 if inrange(Sun2000niva,550,559)
  replace UtbAr=18 if inrange(Sun2000niva,600,629)
  replace UtbAr=19 if inrange(Sun2000niva,640,649)

  merge 1:1 LopNr using `RTB`y''
  keep if _merge == 3
  drop _merge 
  
  tempfile Lisa`y'
  save `Lisa`y'' 
 
 } 

  use `Lisa1990', clear
  
foreach y in 1991 1994 1998 2002 2006 2010 2014  { 
  append using `Lisa`y'', force
}

 save  "E:\ProjData\UtplaceringsData8594\utp-LisaData", replace

// Merge on data on individuals immigrating bw 1985 and 1994
preserve
  use "E:\ProjData\UtplaceringsData8594\utp-Inv_8594", clear
  
  tostring Civil, gen(sCivil)
  replace sCivil = "OG" if inlist(Civil, 1, 8, 9)
  replace sCivil = "G" if inlist(Civil, 2, 3, 7)
  replace sCivil = "S" if Civil == 4
  replace sCivil = "Ä" if Civil == 5
  
  drop Civil
  rename sCivil sCivil_InvAr
  rename Antal_FLxKom Antal_FLxKom_InvAr 
  rename Storlek_Kom Storlek_Kom_InvAr
  rename Antal_FLxKomVuxen Antal_FLxKomVuxen_InvAr 
  rename Storlek_KomVuxen Storlek_KomVuxen_InvAr
  
  tempfile Inv8594
  save `Inv8594'
restore 

 merge m:1 LopNr using `Inv8594'
 keep if _merge == 3
 drop _merge

// Merge on candidacy data
preserve
  use  "D:\SCB_ConPol\Stata harmonized\NomVald\old\Nominerade.dta", clear
  keep if inlist(valar, 1991, 1994, 1998, 2002, 2006, 2010, 2014)
  bysort LopNr valar: egen mVald = max(vald)
  gen mNom = 1
  bysort LopNr valar: gen occ = _n 
  keep if occ == 1
  
  bysort LopNr valar: egen maxPersRost = max(antpersonrost)
  bysort LopNr valar: egen minListNr = min(nrinom)
  
  gen Ar = valar
  tempfile NomData
  save `NomData'
restore
 
  merge m:1 LopNr Ar using `NomData'
  drop if _merge == 2
  replace mNom = 0 if _merge == 1
  replace mVald = 0 if _merge == 1
  drop _merge 
  
// Add first available information on years of education
preserve  
  use "E:\ProjData\SESData\ses-SESData_9013.dta", clear
  keep LopNr Ar Sun2000niva
  
  gen UtbAr_InvAr=.
  replace UtbAr=7 if Sun2000niva<200
  replace UtbAr=9 if inrange(Sun2000niva,200,299)
  replace UtbAr=9.5 if Sun2000niva==204
  replace UtbAr=10 if inrange(Sun2000niva,310,319)
  replace UtbAr=11 if inrange(Sun2000niva,320,329)
  replace UtbAr=12 if inrange(Sun2000niva,330,339)
  replace UtbAr=13 if inrange(Sun2000niva,410,419)
  replace UtbAr=14 if inrange(Sun2000niva,520,529)
  replace UtbAr=15 if inrange(Sun2000niva,530,539)
  replace UtbAr=16 if inrange(Sun2000niva,540,549)
  replace UtbAr=17 if inrange(Sun2000niva,550,559)
  replace UtbAr=18 if inrange(Sun2000niva,600,629)
  replace UtbAr=19 if inrange(Sun2000niva,640,649)
  
  gen mAr = Ar
  replace mAr = . if UtbAr == .
  drop if mAr == .
  bysort LopNr (Ar): gen occ = _n
  keep if occ == 1
  
  rename Ar UtbInfoAr
  keep LopNr UtbAr_InvAr UtbInfoAr
  
  tempfile UtbInvAr
  save `UtbInvAr'
restore  

  merge m:1 LopNr using `UtbInvAr'
  drop if _merge == 2
  drop _merge
	
  gen ValDag = string(Ar) + "0915"
  destring ValDag, gen(nValDag)
  replace mNom = .  if DodDatum < nValDag
  replace mVald = .  if DodDatum < nValDag
  
  gen tFodArMan = date(string(FodArMan), "YM")
  gen tInvDatum = date(InvDatum, "YMD")
  gen tValDag = date(ValDag, "YMD")
  
  gen TidISverige = (tValDag - tInvDatum) / 365.25
  gen AlderValDag = (tValDag-tFodArMan) / 365.25
  
  gen Rostratt = TidISverige >= 3 & AlderValDag > 18 if TidISverige <. & AlderValDag <.
  replace mNom = . if Rostratt == 0
  replace mVald = . if Rostratt == 0
  
  gen Barn0_15 = Barn0_3 + Barn4_6 + Barn7_10 + Barn11_15
  gen Gift = trim(sCivil) == "G" if !mi(trim(sCivil))
  gen SvMedborg = Medblandnamn == "Sverige"
  
// In some cases the municipalities have changed codes due to "länsbyte" etc. 
// although the municipality borders remained intact. Recode codes to handle this problem.
// Because RTB uses the code in place from 01-01 the coming year (i.e., if municipality is
// changing code 2003-01-01 that codes appear already in RTB 2002) we need to pass a year
// variable that is lagged one year when recoding the muncipality codes.
  
  gen sKommun = string(Forsamling, "%06.0f")
  replace sKommun = substr(sKommun, 1, 4)
  gen sInKommun  = string(InKommun,"%04.0f")
  gen nAr = Ar-1
  
  cd  "C:\Userdata\Shared\Dofiles\DoData\PlacementCandidacy"
  do kkbrott.ado 
  
  kkbrott sKommun nAr, utbrutna("nej")
  drop sKommun nAr
  rename aKommun sKommun
   
  gen FodelseAr = floor(FodArMan/100)
  gen InvAlder = InvAr - FodelseAr
  
  gen double logEnklav = log(Antal_FLxKom-1)
  gen double logEnklavInvAr = log(Antal_FLxKom_InvAr-1)
  
  gen double pEnklav = (Antal_FLxKom-1)/(Storlek_Kom-1)
  gen double pEnklavInvAr = (Antal_FLxKom_InvAr-1)/(Storlek_Kom_InvAr-1)
  
  gen double logpEnklav = log(pEnklav)
  gen double logpEnklavInvAr = log(pEnklavInvAr)
  
  gen double logEnklavVuxen = log(Antal_FLxKomVuxen-1)
  gen double logEnklavVuxenInvAr = log(Antal_FLxKomVuxen_InvAr-1)
  
  gen double pEnklavVuxen = (Antal_FLxKom-1)/(Storlek_KomVuxen-1)
  gen double pEnklavVuxenInvAr = (Antal_FLxKomVuxen_InvAr-1)/(Storlek_KomVuxen_InvAr-1)
  
  gen double logpEnklavVuxen = log(pEnklavVuxen)
  gen double logpEnklavVuxenInvAr = log(pEnklavVuxenInvAr)
  
// Identify the small number of individuals that have immigrated more than once (in two different years)  
  preserve
    use "D:\SCB_ConPol\Stata\RTB\Migrationer.dta", clear
	keep if PostTyp == "Inv"
	gen InvAr = floor(Datum/10000)
	bysort LopNr:  egen forstaInvAr = min(InvAr)
	
	bysort LopNr InvAr: gen occ1 =_n
	keep if occ1 == 1
	
	bysort LopNr: egen AntInv = count(LopNr)
	keep if AntInv > 1
	
	bysort LopNr: gen occ2 =_n
	keep if occ2 == 1
	
	keep LopNr AntInv forstaInvAr
	tempfile multInv 
	save `multInv'
  restore
  
  merge m:1 LopNr using `multInv'
  drop if _merge == 2
  
  replace AntInv = 1 if _merge == 1
  
// Add data on seats to voters ratio for the period 1991-2014
  gen sForsamling = string(Forsamling, "%06.0f")
  gen KommunKod = substr(sForsamling, 1, 4)
  destring KommunKod, replace 
  gen ValAr = Ar
  capture drop _merge
  
  merge m:1 KommunKod ValAr using "E:\ProjData\UtplaceringsData8594\utp-SeatVoteRatio_9114.dta"
  
// Was the individual married at the time of immigration?  
  gen Gift_InvAr = sCivil_InvAr == "G" if !mi(sCivil_InvAr)

  save  "E:\ProjData\UtplaceringsData8594\utp-UtpData", replace
 
// Add info on the number of children below 16 at the time of immigration
  do "C:\Userdata\Shared\Dofiles\DoData\UtplaceringsData\utp-BarnInvAr.do"

  use "E:\ProjData\UtplaceringsData8594\utp-UtpData", clear
  capture drop _merge
  merge m:1 LopNr using "E:\ProjData\UtplaceringsData8594\utp-BarnInvAr"
  replace Barn0_15_InvAr = 0 if _merge == 1
  
  capture drop _merge
  save  "E:\ProjData\UtplaceringsData8594\utp-UtpData", replace
  